package courses;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Random {
    JFrame jf;
    JLabel label1, label2;
    JButton button1, button2;
    JPanel panel1, panel2, panel3;

    class MyThread extends Thread {
        String[] string={"","陈佳扬","戴子扬","段志凯","龚仙贵","桂浩","郭俊麟","郭磊","郭远登",
                "何嘉辉","何嘉翊","贺海旺","洪臻","黄浩霖","黄敏杰","黄志威","蒋翔","焦柏华","李绍政",
                "李伟杰","李武","刘程","刘青","刘治廷","龙杰","龙鹏骐","龙维康","罗步成","罗宇翔","宁剑",
                "欧仁杰","盘锋","彭鹏","苏星","唐恒","唐薪","田闯","王磊","吴宝龙","伍恒","肖盼","肖鑫阳",
                "谢城辉","谢城洋","杨棚凯","钟树涛","周涛","庄星鹏","陈文萍","罗鑫"
        };
        private boolean judge=true;
        @Override
        public void run() {
            while (judge) {
                int a = (int) (1+Math.random() * 49);
                String s = String.valueOf(a);
                label2.setText(s+string[a]);
                System.out.println(a);
            }
        }
        public void setJudge(boolean judge) {
            this.judge = judge;
        }
    }

    public Random() {
        jf = new JFrame("随机点名");
        panel1 = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();
        label1 = new JLabel("随机点名");
        label2 = new JLabel();
        label2.setFont(new Font("楷体", Font.BOLD, 30));
        label2.setForeground(Color.CYAN);
        button1 = new JButton("开始点名");
        button2 = new JButton("结束点名");
    }

    public void handler() {
        MyThread thread = new MyThread();
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Thread(thread).start();
                thread.setJudge(true);
            }
        });
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                thread.setJudge(false);
            }
        });
    }

    public void init() {
        jf.add(panel1);
        panel1.add(label1);
        panel2.add(label2);
        panel3.add(button2);
        panel3.add(button1);
        jf.add(panel1, BorderLayout.NORTH);
        jf.add(panel2, BorderLayout.CENTER);
        jf.add(panel3, BorderLayout.SOUTH);
        handler();
        jf.setVisible(true);
        jf.setSize(300, 200);
    }

    public static void main(String[] args) {
        new Random().init();
    }
}
